package com.atguigu.tingshu.search.consumer;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.search.service.MqOpsService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * Description:
 *
 * @author Yanxinhao on 2025/1/18
 */

@Component
public class SearchInfoListener {
    @Autowired
    private MqOpsService mqOpsService;

    /**
     * 监听 管理专辑上架
     * @param content
     * @param message
     * @param channel
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM),
            value = @Queue(value = MqConst.QUEUE_ALBUM_UPPER),
            key = MqConst.QUEUE_ALBUM_UPPER
    ))
    @SneakyThrows
    public void listenAlbumUpper(String content, Message message, Channel channel){
        if (StringUtils.isEmpty(content)){
            return;
        }
        mqOpsService.listenAlbumUpper(content);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }


    /**
     * 监听 管理专辑下架
     * @param content
     * @param message
     * @param channel
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM),
            value = @Queue(value = MqConst.QUEUE_ALBUM_LOWER),
            key = MqConst.QUEUE_ALBUM_LOWER
    ))
    @SneakyThrows
    public void listenAlbumLower(String content, Message message, Channel channel){
        if (StringUtils.isEmpty(content)){
            return;
        }
        mqOpsService.listenAlbumLower(content);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }



    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM_ES_STATA_UPDATE),
            value = @Queue(value = MqConst.QUEUE_ALBUM_ES_STAT_UPDATE),
            key = MqConst.ROUTING_ALBUM_ES_STAT_UPDATE
    ))
    @SneakyThrows
    public void listenAlbumStatUpdate(String orderNo, Message message, Channel channel){
        if (StringUtils.isEmpty(orderNo)){
            return;
        }
        mqOpsService.listenAlbumStatUpdate(orderNo);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }



}